package com.shenqi.language.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenqi.language.mapper.CategoryMapper;
import com.shenqi.language.domain.Category;
import com.shenqi.language.service.ICategoryService;

/**
 * 词条分类Service业务层处理
 * 
 * @author shenqi
 * @date 2025-08-05
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询词条分类
     * 
     * @param id 词条分类主键
     * @return 词条分类
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询词条分类列表
     * 
     * @param category 词条分类
     * @return 词条分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增词条分类
     * 
     * @param category 词条分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改词条分类
     * 
     * @param category 词条分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除词条分类
     * 
     * @param ids 需要删除的词条分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除词条分类信息
     * 
     * @param id 词条分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public List<Category> selectCategoryListByNullParentId() {
        return categoryMapper.selectCategoryListByNullParentId();
    }

    @Override
    public List<Category> findAllNodesByParentId(Long categoryId) {
        List<Category> result = new ArrayList<>();
        findDescendants(categoryId, result);
        return result;
    }

    @Override
    public List<Category> listChildrenCategory(Long categoryId) {
        return categoryMapper.listChildrenCategory(categoryId);
    }

    private void findDescendants(Long parentId, List<Category> result) {
        List<Category> children = categoryMapper.listChildrenCategory(parentId);
        result.addAll(children);
        for (Category child : children) {
            findDescendants(child.getId(), result);
        }
    }
}
